<!DOCTYPE html>
<html class="client-nojs vector-feature-night-mode-disabled vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-1 vector-feature-appearance-pinned-clientpref-1 vector-sticky-header-enabled" lang="en" dir="ltr"><head>
<meta charset="UTF-8">
<title>Guard (computer science)</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="canonical" href="https://en.wikipedia.org/wiki/Guard_(computer_science)"> <link href="./mw/ext.cite.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/ext.math.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/ext.pygments.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.icons.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.search.codex.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/user.styles.css" rel="stylesheet" type="text/css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" type="text/css" href="./mw/site.styles.css">
<link rel="stylesheet" type="text/css" href="./mw/noscript.css">
<link rel="stylesheet" type="text/css" href="./footer.css">
<link rel="stylesheet" type="text/css" href="./vector-2022.css">
</head>
<body class="skin--responsive skin-vector skin-vector-search-vue mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject page-Guard_computer_science rootpage-Guard_computer_science skin-vector-2022 action-view">
<div class="mw-page-container">
<div class="mw-page-container-inner">
<div class="mw-content-container">
<main id="content" class="mw-body">
<header class="mw-body-header vector-page-titlebar">
<h1 id="firstHeading" class="firstHeading mw-first-heading">
<span id="openzim-page-title" class="mw-page-title-main"><span class="mw-page-title-main">Guard (computer science)</span></span>
</h1>
</header>
<a id="top"></a>
<div id="bodyContent" class="vector-body ve-init-mw-desktopArticleTarget-targetContainer" aria-labelledby="firstHeading" data-mw-ve-target-container="">
<div id="mw-content-text" class="mw-body-content mw-content-ltr" lang="en" dir="ltr"><div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr">
<p>In <a href="Computer_programming" title="Computer programming">computer programming</a>, a <b>guard</b> is a <a href="Boolean_expression" title="Boolean expression">Boolean expression</a> that must evaluate to true if the <a href="Execution_(computing)" title="Execution (computing)">execution</a> of the <a href="Computer_program" title="Computer program">program</a> is to continue in the branch in question. Regardless of which <a href="Programming_language" title="Programming language">programming language</a> is used, a <b>guard clause</b>, <b>guard code</b>, or <b>guard statement</b> is a check of integrity <a href="Precondition" title="Precondition">preconditions</a> used to avoid errors during execution.
</p><p>The term <b>guard clause</b> is a <a href="Software_design_pattern" title="Software design pattern">Software design pattern</a> attributed to <a href="Kent_Beck" title="Kent Beck">Kent Beck</a> who codified many often unnamed coding practices into named software design patterns, the practice of using this technique dates back to at least the early 1960's. The <b>guard clause</b> most commonly is added at the beginning of a procedure and is said to "guard" the rest of the procedure by handling edgecases upfront.
</p>
<meta property="mw:PageProp/toc">
<div class="mw-heading mw-heading2"><h2 id="Uses">Uses</h2></div>
<p>A typical example is checking that a <a href="Reference_(computer_science)" title="Reference (computer science)">reference</a> about to be processed is not null, which avoids <a href="Null_pointer" title="Null pointer">null-pointer</a> failures.
</p><p>
Other uses include using a Boolean field for <a href="Idempotence" title="Idempotence">idempotence</a> (so subsequent calls are <a href="NOP_(code)" title="NOP (code)">nops</a>), as in the <a href="Dispose_pattern" title="Dispose pattern">dispose pattern</a>.</p><div class="mw-highlight mw-highlight-lang-java mw-content-ltr" dir="ltr"><pre><span class="kd">public</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="nf">foo</span><span class="p">(</span><span class="n">String</span><span class="w"> </span><span class="n">username</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">username</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="kc">null</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">throw</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">IllegalArgumentException</span><span class="p">(</span><span class="s">"Username is null."</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="c1">// Rest of the method code follows here...</span>
<span class="p">}</span>
</pre></div>
<div class="mw-heading mw-heading3"><h3 id="Flatter_code_with_less_nesting">Flatter code with less nesting</h3></div>
<p>The guard provides an <a href="Early_exit" class="mw-redirect" title="Early exit">early exit</a> from a <a href="Subroutine" class="mw-redirect" title="Subroutine">subroutine</a>, and is a commonly used deviation from <a href="Structured_programming" title="Structured programming">structured programming</a>, removing one level of nesting and resulting in flatter code:<sup id="cite_ref-beck_1-0" class="reference"><a href="#cite_note-beck-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup> replacing <code>if guard { ... }</code> with <code>if not guard: return; ...</code>.
</p><p>Using guard clauses can be a <a href="Refactoring" class="mw-redirect" title="Refactoring">refactoring</a> technique to improve code. In general, less nesting is good, as it simplifies the code and reduces cognitive burden.
</p><p>For example, in <a href="Python_(programming_language)" title="Python (programming language)">Python</a>:
</p>
<div class="mw-highlight mw-highlight-lang-python mw-content-ltr" dir="ltr"><pre><span class="c1"># This function has no guard clause</span>
<span class="k">def</span> <span class="nf">f_noguard</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="nb">int</span><span class="p">):</span>
<span class="c1">#code</span>
<span class="c1">#code</span>
<span class="c1">#code</span>
<span class="k">return</span> <span class="n">x</span> <span class="o">+</span> <span class="mi">1</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="kc">None</span>
<span class="c1"># Equivalent function with a guard clause. Note that most of the code is less indented, which makes it easier to read and reason about</span>
<span class="k">def</span> <span class="nf">f_guard</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="nb">int</span><span class="p">):</span>
<span class="k">return</span> <span class="kc">None</span>
<span class="c1">#code</span>
<span class="c1">#code</span>
<span class="c1">#code</span>
<span class="k">return</span> <span class="n">x</span> <span class="o">+</span> <span class="mi">1</span>
</pre></div>
<p>Another example, written in <a href="C_(programming_language)" title="C (programming language)">C</a>:
</p>
<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="c1">// This function has no guard clause</span>
<span class="kt">int</span><span class="w"> </span><span class="nf">funcNoGuard</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o">>=</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="c1">//code</span>
<span class="w"> </span><span class="c1">//code</span>
<span class="w"> </span><span class="c1">//code</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"> </span>
<span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
<span class="c1">// Equivalent function with a guard clause</span>
<span class="kt">int</span><span class="w"> </span><span class="nf">funcGuard</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="c1">//code</span>
<span class="w"> </span><span class="c1">//code</span>
<span class="w"> </span><span class="c1">//code</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"> </span>
<span class="p">}</span>
</pre></div>
<div class="mw-heading mw-heading2"><h2 id="Terminology">Terminology</h2></div>
<p>The term is used with specific meaning in <a href="APL_(programming_language)" title="APL (programming language)">APL</a>, <a href="Haskell_(programming_language)" class="mw-redirect" title="Haskell (programming language)">Haskell</a>, <a href="Clean_programming_language" class="mw-redirect" title="Clean programming language">Clean</a>, <a href="Erlang_programming_language" class="mw-redirect" title="Erlang programming language">Erlang</a>, <a href="Occam_(programming_language)" title="Occam (programming language)">occam</a>, <a href="Promela" title="Promela">Promela</a>, <a href="OCaml" title="OCaml">OCaml</a>, <a href="Swift_(programming_language)" title="Swift (programming language)">Swift</a>,<sup id="cite_ref-2" class="reference"><a href="#cite_note-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup> <a href="Python_(programming_language)" title="Python (programming language)">Python</a> from version 3.10, and <a href="Scala_(programming_language)" title="Scala (programming language)">Scala</a> programming languages. In <a href="Mathematica" class="mw-redirect" title="Mathematica">Mathematica</a>, guards are called <i>constraints</i>. Guards are the fundamental concept in <a href="Guarded_Command_Language" title="Guarded Command Language">Guarded Command Language</a>, a language in <a href="Formal_methods" title="Formal methods">formal methods</a>. Guards can be used to augment <a href="Pattern_matching" title="Pattern matching">pattern matching</a> with the possibility to skip a pattern even if the structure matches. Boolean expressions in <a href="Conditional_(programming)" class="mw-redirect" title="Conditional (programming)">conditional statements</a> usually also fit this definition of a guard although they are called <i>conditions</i>.
</p>
<div class="mw-heading mw-heading2"><h2 id="Mathematics">Mathematics</h2></div>
<p>In the following Haskell example, the guards occur between each pair of "|" and "=":
</p>
<div class="mw-highlight mw-highlight-lang-haskell mw-content-ltr" dir="ltr"><pre><span class="nf">f</span><span class="w"> </span><span class="n">x</span>
<span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="ow">=</span><span class="w"> </span><span class="mi">1</span>
<span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">otherwise</span><span class="w"> </span><span class="ow">=</span><span class="w"> </span><span class="mi">0</span>
</pre></div>
<p>This is similar to the respective mathematical notation:
</p><p><span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle f(x)=\left\{{\begin{matrix}1&{\mbox{if }}x>0\\0&{\mbox{otherwise}}\end{matrix}}\right.}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>f</mi>
<mo stretchy="false">(</mo>
<mi>x</mi>
<mo stretchy="false">)</mo>
<mo>=</mo>
<mrow>
<mo>{</mo>
<mrow class="MJX-TeXAtom-ORD">
<mtable rowspacing="4pt" columnspacing="1em">
<mtr>
<mtd>
<mn>1</mn>
</mtd>
<mtd>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="false" scriptlevel="0">
<mtext>if </mtext>
</mstyle>
</mrow>
<mi>x</mi>
<mo>></mo>
<mn>0</mn>
</mtd>
</mtr>
<mtr>
<mtd>
<mn>0</mn>
</mtd>
<mtd>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="false" scriptlevel="0">
<mtext>otherwise</mtext>
</mstyle>
</mrow>
</mtd>
</mtr>
</mtable>
</mrow>
<mo fence="true" stretchy="true" symmetric="true"></mo>
</mrow>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle f(x)=\left\{{\begin{matrix}1&{\mbox{if }}x>0\\0&{\mbox{otherwise}}\end{matrix}}\right.}</annotation>
</semantics>
</math></span><img src="./62347de4b635ba2e610fcb648be075768c081346.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -2.505ex; width:23.073ex; height:6.176ex;" alt="{\displaystyle f(x)=\left\{{\begin{matrix}1&{\mbox{if }}x>0\\0&{\mbox{otherwise}}\end{matrix}}\right.}" loading="lazy"></span>
</p><p>In this case the guards are in the "if" and "otherwise" clauses.
</p>
<div class="mw-heading mw-heading2"><h2 id="Multiple_guards">Multiple guards</h2></div>
<p>If there are several parallel guards, they are normally tried in a top-to-bottom order, and the branch of the first to pass is chosen. Guards in a list of cases are typically parallel.
</p><p>However, in Haskell <a href="List_comprehension" title="List comprehension">list comprehensions</a> the guards are in series, and if any of them fails, the list element is not produced. This would be the same as combining the separate guards with <a href="Logical_conjunction" title="Logical conjunction">logical AND</a>, except that there can be other list comprehension clauses among the guards.
</p>
<div class="mw-heading mw-heading2"><h2 id="Evolution">Evolution</h2></div>
<p>A simple conditional expression, already present in <a href="CPL_programming_language" class="mw-redirect" title="CPL programming language">CPL</a> in 1963, has a guard on first sub-expression, and another sub-expression to use in case the first one cannot be used. Some common ways to write this:
</p>
<pre>(x>0) -> 1/x; 0
x>0 ? 1/x : 0
</pre>
<p>If the second sub-expression can be a further simple conditional expression, we can give more alternatives to try before the last <i>fall-through</i>:
</p>
<pre>(x>0) -> 1/x; (x<0) -> -1/x; 0
</pre>
<p>In 1966 <a href="ISWIM" title="ISWIM">ISWIM</a> had a form of conditional expression without an obligatory fall-through case, thus separating guard from the concept of choosing either-or. In the case of ISWIM, if none of the alternatives could be used, the value was to be <i>undefined</i>, which was defined to never compute into a value.
</p><p><a href="Kent_Recursive_Calculator" title="Kent Recursive Calculator">KRC</a>, a "miniaturized version"<sup id="cite_ref-3" class="reference"><a href="#cite_note-3"><span class="cite-bracket">[</span>3<span class="cite-bracket">]</span></a></sup> of <a href="SASL_programming_language" class="mw-redirect" title="SASL programming language">SASL</a> (1976), was one of the first programming languages to use the term "guard". Its function definitions could have several clauses, and the one to apply was chosen based on the guards that followed each clause:
</p>
<div class="mw-highlight mw-highlight-lang-haskell mw-content-ltr" dir="ltr"><pre><span class="w"> </span><span class="n">fac</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="ow">=</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="ow">=</span><span class="w"> </span><span class="mi">0</span>
<span class="w"> </span><span class="ow">=</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">fac</span><span class="w"> </span><span class="p">(</span><span class="n">n</span><span class="o">-</span><span class="mi">1</span><span class="p">),</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="mi">0</span>
</pre></div>
<p>Use of guard clauses, and the term "guard clause", dates at least to <a href="Smalltalk" title="Smalltalk">Smalltalk</a> practice in the 1990s, as codified by <a href="Kent_Beck" title="Kent Beck">Kent Beck</a>.<sup id="cite_ref-beck_1-1" class="reference"><a href="#cite_note-beck-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup>
</p><p>
In 1996, Dyalog APL adopted an alternative pure functional style in which the guard is the only control structure.<sup id="cite_ref-4" class="reference"><a href="#cite_note-4"><span class="cite-bracket">[</span>4<span class="cite-bracket">]</span></a></sup> This example, in APL, computes the parity of the input number:</p><div class="mw-highlight mw-highlight-lang-apl mw-content-ltr" dir="ltr"><pre><span class="nv">parity</span><span class="kd">←</span><span class="kt">{</span>
<span class="w"> </span><span class="m">2</span><span class="o">∣</span><span class="bp">⍵</span><span class="w"> </span><span class="bp">:</span><span class="w"> </span><span class="s1">'odd'</span>
<span class="w"> </span><span class="s1">'even'</span>
<span class="w"> </span><span class="kt">}</span>
</pre></div>
<div class="mw-heading mw-heading2"><h2 id="Pattern_guard">Pattern guard</h2></div>
<p>In addition to a guard attached to a pattern, <b>pattern guard</b> can refer to the use of <a href="Pattern_matching" title="Pattern matching">pattern matching</a> in the context of a guard. In effect, a match of the pattern is taken to mean pass. This meaning was introduced in a proposal for Haskell by <a href="Simon_Peyton_Jones" title="Simon Peyton Jones">Simon Peyton Jones</a> titled <a rel="nofollow" class="external text" href="https://web.archive.org/web/20160203203811/http://research.microsoft.com/en-us/um/people/simonpj/Haskell/guards.html">A new view of guards</a> in April 1997 and was used in the implementation of the proposal. The feature provides the ability to use patterns in the guards of a pattern.
</p><p>An example in extended Haskell:
</p>
<div class="mw-highlight mw-highlight-lang-haskell mw-content-ltr" dir="ltr"><pre><span class="w"> </span><span class="n">clunky</span><span class="w"> </span><span class="n">env</span><span class="w"> </span><span class="n">var1</span><span class="w"> </span><span class="n">var2</span>
<span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="kt">Just</span><span class="w"> </span><span class="n">val1</span><span class="w"> </span><span class="ow"><-</span><span class="w"> </span><span class="n">lookup</span><span class="w"> </span><span class="n">env</span><span class="w"> </span><span class="n">var1</span>
<span class="w"> </span><span class="p">,</span><span class="w"> </span><span class="kt">Just</span><span class="w"> </span><span class="n">val2</span><span class="w"> </span><span class="ow"><-</span><span class="w"> </span><span class="n">lookup</span><span class="w"> </span><span class="n">env</span><span class="w"> </span><span class="n">var2</span>
<span class="w"> </span><span class="ow">=</span><span class="w"> </span><span class="n">val1</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">val2</span>
<span class="w"> </span><span class="c1">-- ...other equations for clunky...</span>
</pre></div>
<p>This would read: "Clunky for an environment and two variables, <i>in case the lookups of the variables from the environment produce values</i>, is the sum of the values. ..." As in <a href="List_comprehension" title="List comprehension">list comprehensions</a>, the guards are in series, and if any of them fails the branch is not taken.
</p>
<div class="mw-heading mw-heading2"><h2 id="See_also">See also</h2></div>
<ul><li><a href="Assertion_(computing)" class="mw-redirect" title="Assertion (computing)">Assertion</a></li>
<li><a href="Guarded_Command_Language" title="Guarded Command Language">Guarded Command Language</a>, a programming language based on non-deterministic conditionals</li>
<li><a href="Guarded_suspension" title="Guarded suspension">Guarded suspension</a></li>
<li><a href="Iverson_bracket" title="Iverson bracket">Iverson bracket</a></li>
<li><a href="Logical_conditional" class="mw-redirect" title="Logical conditional">Logical conditional</a></li>
<li><a href="Sentinel_node" title="Sentinel node">Sentinel node</a>, an object to represent the end of a data structure</li>
<li><a href="Switch_statement" title="Switch statement">Switch statement</a></li></ul>
<div class="mw-heading mw-heading2"><h2 id="References">References</h2></div>
<style data-mw-deduplicate="TemplateStyles:r1239543626">
/* start https://en.wikipedia.org/ */
.mw-parser-output .reflist{margin-bottom:0.5em;list-style-type:decimal}@media screen{.mw-parser-output .reflist{font-size:90%}}.mw-parser-output .reflist .references{font-size:100%;margin-bottom:0;list-style-type:inherit}.mw-parser-output .reflist-columns-2{column-width:30em}.mw-parser-output .reflist-columns-3{column-width:25em}.mw-parser-output .reflist-columns{margin-top:0.3em}.mw-parser-output .reflist-columns ol{margin-top:0}.mw-parser-output .reflist-columns li{page-break-inside:avoid;break-inside:avoid-column}.mw-parser-output .reflist-upper-alpha{list-style-type:upper-alpha}.mw-parser-output .reflist-upper-roman{list-style-type:upper-roman}.mw-parser-output .reflist-lower-alpha{list-style-type:lower-alpha}.mw-parser-output .reflist-lower-greek{list-style-type:lower-greek}.mw-parser-output .reflist-lower-roman{list-style-type:lower-roman}
/* end https://en.wikipedia.org/ */
</style><div class="reflist">
<div class="mw-references-wrap"><ol class="references">
<li id="cite_note-beck-1"><span class="mw-cite-backlink">^ <a href="#cite_ref-beck_1-0"><sup><i><b>a</b></i></sup></a> <a href="#cite_ref-beck_1-1"><sup><i><b>b</b></i></sup></a></span> <span class="reference-text"><style data-mw-deduplicate="TemplateStyles:r1238218222">
/* start https://en.wikipedia.org/ */
.mw-parser-output cite.citation{font-style:inherit;word-wrap:break-word}.mw-parser-output .citation q{quotes:"\"""\"""'""'"}.mw-parser-output .citation:target{background-color:rgba(0,127,255,0.133)}.mw-parser-output .id-lock-free.id-lock-free a{background:url("./mw/Lock-green.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-limited.id-lock-limited a,.mw-parser-output .id-lock-registration.id-lock-registration a{background:url("./mw/Lock-gray-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-subscription.id-lock-subscription a{background:url("./mw/Lock-red-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .cs1-ws-icon a{background:url("./mw/Wikisource-logo.svg")right 0.1em center/12px no-repeat}body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-free a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-limited a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-registration a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-subscription a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .cs1-ws-icon a{background-size:contain;padding:0 1em 0 0}.mw-parser-output .cs1-code{color:inherit;background:inherit;border:none;padding:inherit}.mw-parser-output .cs1-hidden-error{display:none;color:var(--color-error,#d33)}.mw-parser-output .cs1-visible-error{color:var(--color-error,#d33)}.mw-parser-output .cs1-maint{display:none;color:#085;margin-left:0.3em}.mw-parser-output .cs1-kern-left{padding-left:0.2em}.mw-parser-output .cs1-kern-right{padding-right:0.2em}.mw-parser-output .citation .mw-selflink{font-weight:inherit}@media screen{.mw-parser-output .cs1-format{font-size:95%}html.skin-theme-clientpref-night .mw-parser-output .cs1-maint{color:#18911f}}@media screen and (prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .cs1-maint{color:#18911f}}
/* end https://en.wikipedia.org/ */
</style><cite id="CITEREFBeck1997" class="citation book cs1"><a href="Kent_Beck" title="Kent Beck">Beck, Kent</a> (1997). "Guard Clause". <i>Smalltalk Best Practice Patterns,</i>. pp. <span class="nowrap">178–</span>179.</cite></span>
</li>
<li id="cite_note-2"><span class="mw-cite-backlink"><b><a href="#cite_ref-2">^</a></b></span> <span class="reference-text"><cite id="CITEREFCook" class="citation web cs1">Cook, Nate. <a rel="nofollow" class="external text" href="http://nshipster.com/guard-and-defer/">"guard & defer"</a>. <i>NSHipster</i><span class="reference-accessdate">. Retrieved <span class="nowrap">2016-02-26</span></span>.</cite></span>
</li>
<li id="cite_note-3"><span class="mw-cite-backlink"><b><a href="#cite_ref-3">^</a></b></span> <span class="reference-text"><cite id="CITEREFTurner" class="citation web cs1">Turner, D. A. <a rel="nofollow" class="external text" href="https://www.cs.kent.ac.uk/people/staff/dat/tfp12/tfp12.pdf">"Some History of Functional Programming Languages"</a> <span class="cs1-format">(PDF)</span>.</cite></span>
</li>
<li id="cite_note-4"><span class="mw-cite-backlink"><b><a href="#cite_ref-4">^</a></b></span> <span class="reference-text"><cite id="CITEREFScholes" class="citation web cs1">Scholes, John. <a rel="nofollow" class="external text" href="http://www.dyalog.com/uploads/documents/Papers/dfns.pdf">"Direct Functions in Dyalog APL"</a> <span class="cs1-format">(PDF)</span>.</cite></span>
</li>
</ol></div></div>
<div class="mw-heading mw-heading2"><h2 id="External_links">External links</h2></div>
<ul><li><a rel="nofollow" class="external text" href="http://foldoc.org/guard">Guard</a> in <i>Free On-Line Dictionary of Computing - FOLDOC</i>, Denis Howe (editor).</li>
<li><a rel="nofollow" class="external text" href="http://c2.com/cgi/wiki?GuardClause">Guard Clause</a>, <a href="WikiWikiWeb" title="WikiWikiWeb">WikiWikiWeb</a></li>
<li><i>The Haskell 98 Report</i>, chapter <a rel="nofollow" class="external text" href="http://haskell.org/onlinereport/exps.html">3 Expressions</a>.</li>
<li><i>The Mathematica Book,</i> section <a rel="nofollow" class="external text" href="https://web.archive.org/web/20050408114902/http://documents.wolfram.com/mathematica/book/section-2.3.5">2.3.5 Putting Constraints on Patterns</a></li>
<li><i>The Glorious Glasgow Haskell Compilation System User's Guide</i>, Version 6.4, section <a rel="nofollow" class="external text" href="https://web.archive.org/web/20051129140339/http://www.haskell.org/ghc/docs/latest/html/users_guide/syntax-extns.html#pattern-guards">7.3.2. Pattern guards</a></li></ul></div><!--htdig_noindex--><div><div class="zim-footer">
This article is issued from <a class="external text" title="Last edited on 2025-05-15" href="https://en.wikipedia.org/wiki/?title=Guard_(computer_science)&oldid=1290517783">Wikipedia</a>. The text is available under <a class="external text" href="https://creativecommons.org/licenses/by-sa/4.0/deed.en">Creative Commons Attribution-Share Alike 4.0</a> unless otherwise noted. Additional terms may apply for the media files.
</div>
</div><!--/htdig_noindex--></div>
</div>
</main>
</div>
</div>
</div>
</body></html>